package com.ruoyi.weiriya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.weiriya.domain.WArea;
import com.ruoyi.weiriya.domain.WCompany;
import com.ruoyi.weiriya.mapper.WCompanyMapper;
import com.ruoyi.weiriya.service.IWAreaService;
import com.ruoyi.weiriya.service.IWCompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公司信息Service业务层处理
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Service
public class WCompanyServiceImpl extends ServiceImpl<WCompanyMapper, WCompany> implements IWCompanyService
{

    @Autowired
    private IWAreaService areaService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysUserService userService;

    @Override
    public List<WArea> getAreaListByCompanyId(String id) {
        return areaService.list(new LambdaQueryWrapper<WArea>().eq(WArea::getCompanyId, id));
    }

    @Override
    public List<String> getAreaIdList() {
        String companyId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        List<WArea> areaListByCompanyId = this.getAreaListByCompanyId(companyId);
        if(StringUtils.isNull(areaListByCompanyId) || areaListByCompanyId.size() < 1){
            return new ArrayList<>();
        }
        return areaListByCompanyId.stream().map(WArea::getId).collect(Collectors.toList());
    }

    @Transactional
    @Override
    public boolean saveWCompany(WCompany wCompany) {

        this.save(wCompany);
        // 新增公司一级部门
        SysDept sysDept = new SysDept();
        sysDept.setCompanyId(wCompany.getId());
        sysDept.setDeptName(wCompany.getName());
        sysDept.setParentId(0L);
        sysDept.setAncestors("0");
        sysDeptService.companyInsertDept(sysDept);
        return true;
    }

    @Transactional
    @Override
    public boolean removeWCompanyByIds(List<String> ids) {
        long count = areaService.count(new LambdaQueryWrapper<WArea>()
                .in(WArea::getCompanyId, ids));
        if(count > 0){
            throw new ServiceException("已绑定点位，不可删除!");
        }
        SysUser sysUser = new SysUser();
        for (String id : ids) {
            sysUser.setCompanyId(id);
            List<SysUser> sysUsers = userService.selectUserList(sysUser);
            if(sysUsers.size() > 0){
                throw new ServiceException("已绑定管理员，不可删除!");
            }
        }

        sysDeptService.deleteDeptByCompanyIds(ids);
        return this.removeByIds(ids);
    }
}
